.TH std::basic_string::data 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::basic_string::data \- std::basic_string::data

.SH Synopsis
   const CharT* data() const; \fB(1)\fP (noexcept since C++11)
                                  (constexpr since C++20)
   CharT* data() noexcept;    \fB(2)\fP \fI(since C++17)\fP
                                  (constexpr since C++20)

   Returns a pointer to the underlying array serving as character storage. The pointer
   is such that the range

   [data(), data() + size()) \fI(until C++11)\fP
   [data(), data() + size()] \fI(since C++11)\fP

   is valid and the values in it correspond to the values stored in the string.

   The returned array is not required to be null-terminated.
                                                                          \fI(until C++11)\fP
   If empty() returns true, the pointer is a non-null pointer that should
   not be dereferenced.
   The returned array is null-terminated, that is, data() and c_str()
   perform the same function.
                                                                          \fI(since C++11)\fP
   If empty() returns true, the pointer points to a single null
   character.

   The pointer obtained from data() may be invalidated by:

     * Passing a non-const reference to the string to any standard library function, or
     * Calling non-const member functions on the string, excluding operator[](), at(),
       front(), back(), begin(), end(), rbegin(), rend().
   1) Modifying the character array accessed through the const overload of data has
   undefined behavior.
   2) Modifying the past-the-end null terminator stored at data() + size() to any value
   other than CharT() has undefined behavior.

.SH Parameters

   \fI(none)\fP

.SH Return value

   A pointer to the underlying character storage.

   data()[i] == operator[](i) for every i in [0, size()).                 \fI(until C++11)\fP
   data() + i == std::addressof(operator[](i)) for every i in             \fI(since C++11)\fP
   [0, size()].

.SH Complexity

   Constant.

.SH Example


// Run this code

 #include <algorithm>
 #include <cassert>
 #include <cstring>
 #include <string>

 int main()
 {
     std::string const s("Emplary");
     assert(s.size() == std::strlen(s.data()));
     assert(std::equal(s.begin(), s.end(), s.data()));
     assert(std::equal(s.data(), s.data() + s.size(), s.begin()));
     assert('\\0' == *(s.data() + s.size()));
 }

.SH See also

   front accesses the first character
   (DR*) \fI(public member function)\fP
   back  accesses the last character
   (DR*) \fI(public member function)\fP
   c_str returns a non-modifiable standard C character array version of the string
         \fI(public member function)\fP
   data  returns a pointer to the first character of a view
         \fI(public member function of std::basic_string_view<CharT,Traits>)\fP
